﻿@{
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<form id="form1" runat="server" method="post" style="height: 100%">
    <div class="mini-splitter" style="width: 100%; height: 100%;">
        <div size="500" showcollapsebutton="true">
            <div class="mini-toolbar" style="padding: 2px; border-top: 0; border-left: 0; border-right: 0;">
                <a class="mini-button" id="cnew" iconcls="icon-edit" onclick="addSeckillClass" plain="true">增加</a>
                <a class="mini-button" id="modify" name="modify" iconcls="icon-edit" onclick="modify" plain="true">修改</a>
                <a class="mini-button" id="cdel" iconcls="icon-remove" onclick="delSeckillClass" plain="true">删除</a>
                <a class="mini-button" iconcls="icon-search" onclick="getClassData" plain="true">刷新</a>
            </div>
            <div class="mini-toolbar" style="padding: 2px; border-top: 0; border-left: 0; border-right: 0;">
                <table>
                    <tr>
                        <td>
                            <div class="item">
                                <label class="label">门店:</label>
                                <div style="display: inline-block; vertical-align: middle;">@Html.Partial("~/Views/Modular/LookupStockInfo.cshtml")</div>
                            </div>
                        </td>
                    </tr>
                </table>
            </div>
            <div class="mini-fit">
                <div id="datagrid0" class="mini-datagrid" style="width: 100%; height: 100%;" pagesize="20"
                    borderstyle="border:1;" url="/Coupon/GetSeckillSetClass"
                    sortmode="client" selectonload="true" onrowclick="loadSeckillGoods" onload="loadSeckillGoods">
                    <div property="columns">
                        <div type="checkcolumn" width="40">序号</div>
                        <div field="FItemID" name="FItemID" width="80" visible="false" headeralign="center">内码</div>
                        <div field="FStartDate" name="FStartDate" width="100" headeralign="center" dateformat="yyyy-MM-dd HH:mm:ss">开始时间</div>
                        <div field="FEndDate" name="FEndDate" width="100" headeralign="center" dateformat="yyyy-MM-dd HH:mm:ss">结束时间</div>
                        <div field="FStockName" name="FStockName" width="100" headeralign="center">门店</div>
                        <div type="checkboxcolumn" field="FIsEnabled" name="FIsEnabled" truevalue="1" falsevalue="0" width="50" headeralign="center">是否启用</div>
                    </div>
                </div>
            </div>
        </div>

        <div showcollapsebutton="true">
            <div class="mini-toolbar" style="padding: 2px; border-top: 0; border-left: 0; border-right: 0;">
                <table width="100%;">
                    <tr>
                        <td width="1000px;">
                            <a class="mini-button" name="new" iconcls="icon-new" onclick="addGoods" plain="true">增加</a>
                            <a class="mini-button" name="save" id="save" iconcls="icon-save" onclick="SaveData" plain="true">保存</a>
                            <a class="mini-button" name="del" iconcls="icon-remove" onclick="delGoods" plain="true">删除</a>
                            <a class="mini-button" iconcls="icon-close" onclick="CloseWindow" plain="true">关闭</a>
                        </td>
                    </tr>
                </table>
            </div>
            <div id="tabs1" class="mini-tabs" activeindex="0" style="width: 100%; height: 100%; top: 0px; left: 0px;" plain="false">
                <div name="tabList" title="秒杀商品列表">
                    <div id="datagrid1" class="mini-datagrid" style="width: 100%; height: 95%;" pagesize="20"
                        borderstyle="border:1;" url="/Coupon/GetSeckillGoods"
                        allowcellselect="true" allowcelledit="true" multiselect="true" showmodified="true" oncellcommitedit="onCellCommitEdit">
                        <div property="columns">
                            <div type="checkcolumn" width="40">选择</div>
                            <div field="FItemID" name="FItemID" width="80" visible="false" headeralign="center">内码</div>
                            <div field="FGoodsID" name="FGoodsID" width="80" visible="false" headeralign="center">商品id</div>
                            <div field="FSeckillQty" name="FSeckillQty" width="60" headeralign="center">
                                秒杀数量
                                <input property="editor" class="mini-textbox" style="width: 100%;" />
                            </div>
                            <div field="FSeckillPrce" name="FSeckillPrce" width="100" numberformat="#,0.00" headeralign="center">
                                秒杀价
                                <input property="editor" class="mini-textbox" style="width: 100%;" />
                            </div>
                            <div field="FName" name="FName" width="150" headeralign="center">商品名称</div>
                            <div field="FCode" name="FCode" width="120" headeralign="center">条码</div>
                            <div field="FNumber" name="FNumber" width="100" headeralign="center">编码</div>
                            <div field="FSpec" name="FSpec" width="90" headeralign="center">规格</div>
                            <div field="FPosPrice" name="FPosPrice" width="90" numberformat="#,0.00" headeralign="center">零售价</div>
                            <div field="FMallPrice" name="FMallPrice" width="90" numberformat="#,0.00" headeralign="center">电商价</div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</form>
<script src="~/Scripts/jquery-1.11.1.min.js"></script>
<script src="~/Scripts/common.js"></script>
<script>
    mini.parse();
    var grid = mini.get("datagrid0");
    var grid1 = mini.get("datagrid1");
    function addSeckillClass() {
        mini.open({
            url: "/Coupon/SeckillClassInfo",
            title: "秒杀详情", width: 650, height: 400,
            onload: function () {
                //                var iframe = this.getIFrameEl();
                //                var data = { FItemID: 0 };
                //                iframe.contentWindow.SetData(data);
            },
            ondestroy: function (action) {
                LoadListData();
            }
        });
    }

    function modify() {
        mini.open({
            url: "/Coupon/SeckillClassInfo",
            title: "秒杀详情", width: 650, height: 400,
            onload: function () {
                var iframe = this.getIFrameEl();
                var row = grid.getSelected();
                var data = { FItemID: row.FItemID };
                iframe.contentWindow.SetData(data);
            },
            ondestroy: function (action) {
                LoadListData();
            }
        });
    }

    function delSeckillClass() {

        var row = grid.getSelected();
        if (row) {
            if (confirm("确定删除？")) {
                var param = {
                    FItemID: row.FItemID
                }
                postRequest("/Coupon/DelSeckillClass", param, function (res) {
                    if (res.status == 1) {
                        LoadListData();
                    } else {
                        mini.showTips({ content: res.msg, state: "danger", x: "center", y: "top", timeout: 3000 });
                    }
                });
            }
        } else {
            alert("请先选择一个分类!");
        }
    }

    function getClassData() {
        LoadListData();
    }

    function LoadListData() {
        var sSystemAppID = parent.window.getSystemAppID();
        var param = {
            sSystemAppID: sSystemAppID,
            FStockID: mini.get('FStockID').getValue()
        }
        grid.load(param);
    }

    function loadSeckillGoods() {
        var row = grid.getSelected();
        var param = {
            FClassID: row.FItemID
        }
        grid1.load(param);
        mini.get("save").setEnabled(false);
    }

    function addGoods() {
        var goodrow = grid1.getData();
        if (goodrow.length > 0) {
            mini.alert("秒杀商品已存在");
            return;
        }
        var row = grid.getSelected();
        if (row) {
            mini.open({
                url: "../Bill/GetBillGoods",
                title: "选择商品",
                width: 900,
                height: 500,
                onload: function() {
                    var iframe = this.getIFrameEl();
                    var data = { key: "", mBillType: "0" };
                    iframe.contentWindow.SetData(data);
                },
                ondestroy: function(action) {
                    if (action == "close") return;
                    if (action) {
                        if (action.length == undefined) return;
                        if (action.length > 1) {
                            mini.alert("每任务只能选择一个秒杀商品");
                            return;
                        }
                        for (var i = 0, l = action.length; i < l; i++) {
                            var rw = action[i];
                            var newRow = {
                                FName: rw.FName,
                                FCode: rw.FCode,
                                FNumber: rw.FNumber,
                                FSpec: rw.FSpec,
                                FPosPrice: rw.FPosPrice,
                                FMallPrice: rw.FPrice,
                                FGoodsID: rw.FItemID
                            };
                            grid1.addRow(newRow, grid1.getData().length);
                            mini.get("save").setEnabled(true);
                        }
                    }
                }
            });
        } else {
            alert("请先选择一个分类!");
        }
    }

    function delGoods() {
        var row = grid1.getSelecteds();
        if (row) {
            if (confirm("确定删除？")) {
                var param = {
                    data: JSON.stringify(row)
                }
                postRequest("/Coupon/DelSeckillGoods", param, function (res) {
                    if (res.status == 1) {
                        LoadListData();
                    } else {
                        mini.showTips({ content: res.msg, state: "danger", x: "center", y: "top", timeout: 3000 });
                    }
                });
            }
        } else {
            alert("请先选择数据!");
        }
    }

    function SaveData() {
        mini.get("save").setEnabled(false);
        var row = grid1.getChanges();//row._state为added/modified/removed。
        var parentId = grid.getSelected().FItemID;
        if (row) {
            var param = {
                data: JSON.stringify(row),
                parentId: parentId
            }
            postRequest("/Coupon/SaveSeckillGoods", param, function (res) {
                if (res.status == 1) {
                    loadSeckillGoods();
                    mini.showTips({ content: "保存成功。", state: "success", x: "center", y: "top", timeout: 3000 });
                } else {
                    mini.showTips({ content: res.msg, state: "danger", x: "center", y: "top", timeout: 3000 });
                }
            });
        }
    }


    //提交单元格编辑数据前激发
    function onCellCommitEdit(e) {
        mini.get("save").setEnabled(true);//编辑后保存按钮可用  
    }

    function CloseWindow(action) {
        if (window.CloseOwnerWindow) window.CloseOwnerWindow(action);
        else window.close();
    }

    LoadListData();
</script>
